package com.lostandfound.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.lostandfound.entity.Plate;
import com.lostandfound.entity.User;
import com.lostandfound.service.PlateService;
//import com.lostandfound.service.TopicService;
import com.lostandfound.util.ReturnCode;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**  
* 
* @date 2018年3月11日
*/
@ParentPackage("basePackage")
@Namespace("/")
/*@Results({
	@Result(name="success",location="/success.jsp"),
	@Result(name="failure",location="/fail.jsp")
})*/
public class PlateAction {
	
	@Autowired
	private PlateService plateService;
	/*@Autowired
	private TopicService topicService;*/
	
	private Plate plate;//对象模型
	private Map<String, Object> res = new HashMap<String, Object>();//返回json信息
	private HashMap<String, Object> pagemap;//分页信息
	
	public Map<String, Object> getRes() {
		return res;
	}
	public void setRes(Map<String, Object> res) {
		this.res = res;
	}
	
	public HashMap<String, Object> getPagemap() {
		return pagemap;
	}
	public void setPagemap(HashMap<String, Object> pagemap) {
		this.pagemap = pagemap;
	}

	public Plate getPlate() {
		return plate;
	}
	public void setPlate(Plate plate) {
		this.plate = plate;
	}
	
	/**
	 * 获取session中保存的当前登录用户信息
	 * @return
	 */
	private User getUser(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		User loginUser = (User)session.get("loginUser");
		return loginUser;
	}

	/**
	 * 判断是否是管理员
	 * @return
	 */
	@SuppressWarnings("unused")
	private Boolean isManager(){
		User user = getUser();
		if(user != null){
			if(user.getType() == "1" || user.getType() == "2"){
				return true;
			}
		}
		return false;
	}

	/**
	 * 获取所有的板块
	 * @return
	 */
	@Action(value="getAllPlate",results={
			@Result(type="json",params={"root","res"}),
	})
	public String getAllPlate(){
		List<?> plates = plateService.getAllPlate();
		this.res.put("plateAll", plates);
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 获取符合查询条件的结果数量
	 * @return
	 */
	@Action(value="getCount",results={
			@Result(type="json",params={"root","res"}),
	})
	public String getCount(){
		int count = plateService.Count(pagemap);
		this.res.put("count", count);
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 查看板块列表信息
	 * @return
	 */
	@Action(value="listPlate",results={
			@Result(type="json",params={"root","res"}),
	})
	public String listPlate(){
		if(getUser() != null){//判断是否是登录状态
			if(pagemap == null){//pagemap存放分页属性，如果pagemap为空，设置默认每页显示10条数据
				pagemap = new HashMap<String, Object>();
				pagemap.put("page", "1");
				pagemap.put("size", "10");
			}
			try {
				@SuppressWarnings("unchecked")
				List<Plate> listplate = (List<Plate>)plateService.listPlateByPage(pagemap);//分页查询板块信息
				int count = plateService.Count(pagemap);
				this.res.put("plateList", listplate);//获取当页数据
				this.res.put("platecount",count);//获取总条目
				int size = Integer.parseInt((String)pagemap.get("size"));
				this.res.put("platepage",count%size>0?count/size+1:count/size);//获取总页数
				this.res.put("code", ReturnCode.SUCCESS);//查询成功
			} catch (Exception e) {
				System.out.println("查询失败！");
				this.res.put("code", ReturnCode.FAIL);//查询失败，返回主页
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//用户未登录，跳转登录页
		System.out.println("--请登录--");//没有登录则跳转登录页面
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 判断板块是否存在
	 * @return
	 */
	@Action(value="isPlateExist",results={
			@Result(type="json",params={"root","res"}),
	})
	public String isPlateExist(){
		if(this.plate != null && this.plate.getPname() != null){
			if(!plateService.getPlateByName(this.plate.getPname()).isEmpty()){
				res.put("isPlateExist", true);//账号已被占用
			}else{
				res.put("isPlateExist", false);//账号可用
			}
			return ActionSupport.SUCCESS;
		}
		res.put("Code", ReturnCode.LOST_NEWS);//信息丢失
		return ActionSupport.SUCCESS;
	}

	/**
	 * 执行板块信息添加操作
	 * @return
	 */
	@Action(value="addPlate",results={
			@Result(type="json",params={"root","res"}),
	})
	public String addPlate(){
		if(this.plate != null){
			User user = getUser();
			this.plate.setP_create_time(new Date());
			this.plate.setUser(user);
			try {
				plateService.save(this.plate);
				//System.out.println("保存成功!");
				this.res.put("code", ReturnCode.SUCCESS);//添加成功，跳转添加页可继续添加
			} catch (Exception e) {
				System.out.println("添加板块信息失败！");
				this.res.put("code", ReturnCode.FAIL);//添加失败，跳转添加页可继续编辑
				//this.res.put("addPlate", this.plate);
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//添加失败，跳转添加页重新添加
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 根据ID获取板块信息
	 * @return
	 */
	@Action(value="getPlateById",results={
			@Result(type="json", params={"root","res"})
	})
	public String getPlateById(){
		if(this.plate != null){
			Long pid = this.plate.getPid();
			try {
				this.plate = plateService.selectByPrimary(pid);
				this.res.put("plate", this.plate);
				this.res.put("code", ReturnCode.SUCCESS);
			} catch (Exception e) {
				System.out.println("获取板块信息失败！");
				this.res.put("code", ReturnCode.FAIL);
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//输入有误
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 修改板块信息
	 * @return
	 */
	@Action(value="updatePlate",results={
			@Result(type="json",params={"root","res"})
	})
	public String updatePlate(){
		if(this.plate != null){
			try {
				Plate newplate = plateService.selectByPrimary(this.plate.getPid());
				newplate.setP_modify_time(new Date());
				newplate.setPdescription(this.plate.getPdescription());
				newplate.setPname(this.plate.getPname());
				plateService.update(newplate);
				this.res.put("code", ReturnCode.SUCCESS);
			} catch (Exception e) {
				System.out.println("更新板块信息失败！");
				this.res.put("code", ReturnCode.FAIL);
				//this.res.put("updatePlate", this.plate);
			}
			return ActionSupport.SUCCESS;
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//输入有误，板块信息为空，跳转修改页
		return ActionSupport.SUCCESS;
	}
	
	/**
	 * 批量删除板块信息
	 * @return
	 */
	@Action(value="deletePlate",results={
			@Result(type="json",params={"root","res"})
	})
	public String deletePlate(){
		if(this.res != null && this.res.get("ids") != null){
			String[] ids = (String[])this.res.get("ids");
		
			String pids = ids[0]; 
			pids = pids.substring(1, pids.length()-1);
			String[] id = pids.split(",");
			if(id != null && id.length >0){
				Long[] p_ids = new Long[id.length];
				for (int i=0;i<id.length;i++) {
					Long l1 = Long.parseLong(id[i]);
					p_ids[i] = l1;
				}
				try {
					/*for (Long long1 : p_ids) {
						topicService.deleteTopicByPlate(long1);//先删除板块下的主贴
					}*/
					plateService.deletePlateByBranch(p_ids);//再删除板块
					this.res.put("code", ReturnCode.SUCCESS);//删除成功，页面清除指定删除项
				} catch (Exception e) {
					System.out.println("删除用户信息失败！");
					this.res.put("code", ReturnCode.FAIL);//删除失败，请重试
				}
				return ActionSupport.SUCCESS;
			}
		}
		this.res.put("code", ReturnCode.LOST_NEWS);//输入有误,请选择要删除的板块
		return ActionSupport.SUCCESS;
	}
	
	
	

}
